using System;
using System.Web.Mvc;
using System.Web.UI;
using RMH.Entities;
using RMH.Services;
using RMH.Web.Models.Binders;
using System.Linq;
using System.Collections.Generic;
using RMH.Web.Exensions;

namespace RMH.Web.Controllers
{
    public class StayController : ControllerBase
    {
        private IStayService stayService;
		IRoomService roomService;

		public StayController(IStayService stayService, IRoomService roomService)
        {
            this.stayService = stayService;
			this.roomService = roomService;
        }

        //
        // GET: /Stay/

        [Authorize(Roles = "Administrator,Volunteer")]
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Stay/Details/5

        [Authorize(Roles = "Administrator,Volunteer")]
        public ViewResult Details(int? id)
        {
            return View(stayService.Get(id.Value));
        }

        [Authorize(Roles = "Administrator,Volunteer")]
        public ActionResult New(int? id)
        {
            Stay stay = stayService.GetNewStayForPatient(id.Value);
            ViewData["ForPatient"] = stay.Patient.GetFullName();
            stay.CheckinDate = DateTime.Now;
            stay.CheckinBy = this.User.Identity.Name;
            ViewData["Rooms"] = new SelectList(roomService.GetAllForDropdown(), "Id", "RoomNumber");
            SetPaymentDropDowns(stay);

            return View("Edit", stay);
        }

        private void SetPaymentDropDowns(Stay stay)
        {
            PaymentType payType = PaymentType.Cshcs; ;
            PaymentMethod payMethod = PaymentMethod.Cash;
            List<SelectListItem> payTypes = new List<SelectListItem>();
            List<SelectListItem> payMethods = new List<SelectListItem>();

            payTypes.AddRange(payType.ToSelectList());
            payMethods.AddRange(payMethod.ToSelectList());


            if (stay.Payment != null)
            {
                if (stay.Payment.Method != null)
                {
                    SelectListItem payMethodItem = payMethods.Find(p => int.Parse(p.Value) == (int)stay.Payment.Method);
                    if (payMethodItem != null)
                    {
                        payMethodItem.Selected = true;
                    }
                }

                if (stay.Payment.Type != null)
                {
                    SelectListItem payTypeItem = payTypes.Find(p => int.Parse(p.Value) == (int)stay.Payment.Type);
                    if (payTypeItem != null)
                    {
                        payTypeItem.Selected = true;
                    }
                }
            }


            ViewData["PaymentTypes"] = payTypes.AsEnumerable<SelectListItem>();
            ViewData["PaymentMethods"] = payMethods.AsEnumerable<SelectListItem>();
        }

        //
        // GET: /Stay/Edit/5

        [Authorize(Roles = "Administrator,Volunteer")]
        public ActionResult Edit(int? id)
        {
            Stay stay;
            if (!id.HasValue)
            {
                stay = new Stay();
                ViewData["ForPatient"] = "New Person";
            }
            else
            {
                stay = stayService.Get(id.Value);
                ViewData["ForPatient"] = stay.Patient.GetFullName();
            }

            ViewData["Rooms"] = new SelectList(roomService.GetAllForDropdown(), "Id", "RoomNumber", stay.Room != null ? stay.Room.Id : 0);
            
            // put payment types and methods into view data
            SetPaymentDropDowns(stay);

            return View(stay);
        }

        [Authorize(Roles = "Administrator,Volunteer")]
        public ActionResult CheckOut(int? id)
        {
            if (!id.HasValue)
            {
                RedirectToAction("List");
            }

            Stay stay = stayService.Get(id.Value);
            if (stay == null || stay.CheckoutDate != null)
            {
                stay = stayService.Get(id.Value);
            }
            ViewData["ForPatient"] = stay.Patient.GetFullName();
            ViewData["Rooms"] = new SelectList(roomService.GetAllForDropdown(), "Id", "RoomNumber", stay.Room != null ? stay.Room.Id : 0);
            stay.CheckoutDate = DateTime.Now;
            stay.CheckoutBy = this.User.Identity.Name;
            return View("Edit", stay);
        }

        //
        // POST: /Stay/Edit/5

        [Authorize(Roles = "Administrator,Volunteer")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ViewResult Edit([ModelBinder(typeof(StayBinder))] Stay stay)
        {
            SetPaymentDropDowns(stay);

            // check for required fields
            if (stay.CheckinDate == null)
            {
                ModelState.AddModelError("CheckinDate", "Checkin Date is required.");
                ModelState.SetModelValue("CheckinDate", ValueProvider["CheckinDate"]);
            }

            // don't have a hopspital unit
            if (string.IsNullOrEmpty(stay.HospitalUnit))
            {
                ModelState.AddModelError("HospitalUnit", "Hospital Unit is required.");
                ModelState.SetModelValue("HospitalUnit", ValueProvider["HospitalUnit"]);
            }

            // don't have a room?
            if (stay.Room == null)
            {
                ModelState.AddModelError("RoomId", "Room Number is required.");
                ModelState.SetModelValue("RoomId", ValueProvider["RoomId"]);
            }

            // have a payment?
            if (stay.Payment != null)
            {
                if (stay.Payment.Amount != null || stay.Payment.BillingDate != null ||
                    string.IsNullOrEmpty(stay.Payment.CheckNumber) == false)
                {
                    // check if payment method and type are answered
                    if (stay.Payment.Method == PaymentMethod.Unknown)
                    {
                        ModelState.AddModelError("Payment.Method", "Payment Method is required.");
                        ModelState.SetModelValue("Payment.Method", ValueProvider["Payment.Method"]);
                    }

                    if (stay.Payment.Type == PaymentType.Unknown)
                    {
                        ModelState.AddModelError("Payment.Type", "Payment Type is required.");
                        ModelState.SetModelValue("Payment.Type", ValueProvider["Payment.Type"]);
                    }
                }
            }
           
/*
            if (string.IsNullOrEmpty(stay.CheckinBy))
            {
                ModelState.AddModelError("CheckinBy", "Checkin By Staff Member is required.");
                ModelState.SetModelValue("CheckinBy", ValueProvider["CheckinBy"]);
            }
*/

            if (!ModelState.IsValid)
            {
                stayService.Evict(stay);
                if (stay.Patient != null)
                    ViewData["ForPatient"] = stay.Patient.GetFullName();
                if (roomService.GetAllForDropdown() != null)
                    ViewData["Rooms"] = new SelectList(roomService.GetAllForDropdown(), "Id", "RoomNumber", stay.Room != null ? stay.Room.Id : 0);
                return View(stay);
            }

            stayService.Save(stay);
            return View("Saved");
        }

        [Authorize(Roles = "Administrator,Volunteer")]
        public ActionResult List()
        {
            return View(stayService.GetCurrent());
        }



    }
}
